﻿using JetBrains.ReSharper.Daemon.CSharp.Stages;
using JetBrains.ReSharper.Daemon.Stages;
using JetBrains.ReSharper.Daemon.Stages.Dispatcher;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.CSharp.Tree;
using MugenMvvmToolkit.ReSharper.Highlighting;
using MugenMvvmToolkit.ReSharper.Infrastructure;

namespace MugenMvvmToolkit.ReSharper.ViewModels.Analyzers
{
    [ElementProblemAnalyzer(new[] { typeof(ICSharpArgumentsOwner) },
        HighlightingTypes = new[] { typeof(NotEmptyParamsHighlighting) })]
    public class NotEmptyParamsAttributeAnalyzer : ElementProblemAnalyzerBase<ICSharpArgumentsOwner>
    {
        #region Overrides of ElementProblemAnalyzerBase<ICSharpArgument>

        protected override void Run(ICSharpArgumentsOwner element, ElementProblemAnalyzerData data, IHighlightingConsumer consumer)
        {
            var parametersOwner = element.GetReferenceDeclaredElement<IParametersOwner>();
            if (parametersOwner == null)
                return;
            for (int index = 0; index < parametersOwner.Parameters.Count; index++)
            {
                IParameter parameter = parametersOwner.Parameters[index];
                if (!parameter.IsParameterArray || !ToolkitExtensions.HasNotEmptyParamsAttribute(parameter))
                    continue;
                if (element.Arguments.Count <= index || element.Arguments[index].Value.IsNullValue())
                    consumer.AddHighlighting(new NotEmptyParamsHighlighting(element, parameter));
            }
        }

        #endregion
    }
}
